using System;

namespace Xfinity.Maths.Filters
{
    /// <summary>
    /// A simple box filter.
    /// </summary>
    public class BoxFilter : Function1D
    {
        private float radius;
        private float oneOverTwoRadius;

        /// <summary>
        /// Gets or sets the radius.
        /// </summary>
        /// <value>The radius.</value>
        public float Radius
        {
            get { return radius; }
            set
            {
                radius = value;
                oneOverTwoRadius = 1 / (value * 2);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BoxFilter"/> class.
        /// </summary>
        /// <param name="radius">The radius.</param>
        public BoxFilter(float radius)
        {
            this.radius = radius;
        }
        /// <summary>
        /// Evaluates the function at the location.
        /// </summary>
        /// <param name="location">The location.</param>
        /// <returns>The value at the location.</returns>
        public override float Evaluate(float location)
        {
            if (Math.Abs(location) > radius)
            {
                return 0;
            }
            else
            {
                return oneOverTwoRadius;
            }

        }
    }
}
